home *** CD-ROM | disk | FTP | other *** search
- /*--------------------------------------------------------------------------*
- * changefont -- an MPW tool to perform batch formatting on *
- * Think C files. *
- * Copyright © 1989 by Carlos A. Weber, M.D. *
- * Syntax : changefont -f fontname -s size -t tabs [filenames...] *
- * defaults to Courier 10, tab setting 4 *
- *--------------------------------------------------------------------------*/
-
- #include <Types.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <ErrMgr.h>
- #include <CursorCtl.h>
- #include <Errors.h>
- #include <Resources.h>
- #include <Files.h>
- #include <Quickdraw.h>
- #include <Windows.h>
- #include <Fonts.h>
- #include <Memory.h>
-
- #define SPACE 0xCA /* ASCII value for non-breaking space character */
- #define EFF 0x66 /* ASCII value for 'f' */
- #define ESS 0x73 /* ASCII value for 's' */
- #define TEE 0x74 /* ASCII value for 't' */
- #define DEFSZ 10 /* 10 point type unless user chooses otherwise */
- #define DEFTAB 4 /* tab size defaults to 4 */
-
- typedef struct efntData {
- short fSize;
- char fName[256];
- } efntData; /* structure of the EFNT resource type */
-
- typedef struct etabData {
- short mystery;
- short tSize;
- } etabData; /* data structure for the ETAB resource */
-
- static char defFont[] = "\pCourier"; /* default font if not overridden */
-
- static efntData myEFNT;
- static etabData myETAB;
-
- void SyntaxError( short err, char *msg)
- {
- switch (err)
- {
- case 1: printf("# %s is an invalid option\n", msg);
- break;
- case 2: printf("# missing font\n");
- break;
- case 3: printf("# missing font size\n");
- break;
- case 4: printf("# missing tab setting\n");
- break;
- case 5: printf("# %s is an invalid font\n", msg);
- break;
- case 6: printf("# %s is an invalid font size\n", msg);
- break;
- case 7: printf("# %s is an invalid tab size\n", msg);
- break;
- case 8: printf("# the - character must be accompanied by an option\n");
- break;
- case 9: printf("# Usage -- changefont [filenames…] \n");
- printf(" -f fontname # set font of filenames to fontname\n");
- printf(" -s fontsize # set font size of filenames to fontsize\n");
- printf(" -t tabs # set tab setting to tabs\n");
- break;
- }
- exit(1);
- }
-
- void HandleOption(char *opt, short *argIndex, int argc, char *argv[])
- {
- char optionChar = tolower(opt[1]);
- short fNum;
- long biggy;
-
- switch((short)optionChar)
- {
- case EFF :
- if (++(*argIndex) < argc)
- {
- getfnum(argv[*argIndex], &fNum); /* use lower case, pass C string*/
- if (fNum <= 0)
- SyntaxError(5, argv[*argIndex]);
- else
- strcpy(myEFNT.fName, c2pstr(argv[*argIndex]));
- }
- else
- SyntaxError(2, "");
- break;
- case ESS :
- if (++(*argIndex) < argc)
- {
- stringtonum(argv[*argIndex], &biggy); /* use lower case, pass C string*/
- if (biggy <= 0 || biggy >= 128)
- SyntaxError(6, argv[*argIndex]);
- else
- myEFNT.fSize = biggy;
- }
- else
- SyntaxError(3, "");
- break;
- case TEE :
- if (++(*argIndex) < argc)
- {
- stringtonum(argv[*argIndex], &biggy); /* use lower case, pass C string*/
- if (biggy <= 0 || biggy >= 25)
- SyntaxError(7, argv[*argIndex]);
- else
- myETAB.tSize = biggy;
- }
- else
- SyntaxError(4, "");
- break;
- default :
- SyntaxError(1, opt);
- break;
- }
- }
-
- void SkipOption(char *opt, short *argIndex)
- {
- char optionChar = tolower(*(opt+1));
-
- if (optionChar == EFF || optionChar == ESS || optionChar == TEE)
- ++(*argIndex);
- }
-
- void ReadCommandLine(int argc, char *argv[])
- {
- short argVIndex;
- size_t strSize;
- char curArg[256];
-
- if (argc == 1)
- SyntaxError(9,"");
- for (argVIndex = 1; argVIndex < argc; argVIndex++)
- {
- strSize = strlen(strcpy(curArg,argv[argVIndex]));
- if (curArg[0] == '-')
- {
- if (strSize > 1)
- HandleOption(curArg, &argVIndex, argc, argv);
- else
- SyntaxError(8, "");
-
- }
- }
- }
-
- void ReportError(short error, char *filename)
- {
- if (error == noErr) return; /* don't exit for non-errors */
- fprintf(stderr, "ERROR!! ");
- switch (error)
- {
- case -35 : fprintf(stderr, "%s volume does not exist\n", filename);
- break;
- case -36 : fprintf(stderr, "%s IO Error\n", filename);
- break;
- case -37 : fprintf(stderr, "%s is a bad file or volume name\n", filename);
- break;
- case -42 : fprintf(stderr, "Too many files open\n");
- break;
- case -43 : fprintf(stderr, "%s not found\n", filename);
- break;
- case -45 : fprintf(stderr, "%s is locked\n", filename);
- break;
- case -46 : fprintf(stderr, "%s is locked by a software flag\n", filename);
- break;
- case -47 : fprintf(stderr, "%s is busy; one or more files are open\n", filename);
- break;
- case -53 : fprintf(stderr, "%s volume not on-line\n", filename);
- break;
- case -54 : fprintf(stderr, "%s cannot be opened for writing; file is locked\n", filename);
- break;
- case -61 : fprintf(stderr, "%s Read/write permission does not allow writing\n", filename);
- break;
- default: fprintf(stderr, "OS error #%d has occurred.\n", error);
- break;
- }
- exit(2);
- }
-
- short spaceWidth(void) /* first word of ETAB resource is width of a *
- * non-breaking space */
- {
- short result, fontNum;
- WindowPtr dummy;
- Rect dRect;
-
- SetRect(&dRect, 5000, 5000, 5050, 5050);
- dummy = NewWindow(nil, &dRect, "\p", false, noGrowDocProc, (WindowPtr)-1, false, 0L);
- SetPort(dummy); /* all this to get a GrafPort to pseudo-draw in */
- getfnum(myEFNT.fName, &fontNum);
- TextFont(fontNum); /* using our offscreen invisible window */
- TextSize(myEFNT.fSize);
- result = CharWidth(SPACE);
- DisposeWindow(dummy);
- return result;
- }
-
- void main(int argc, char *argv[])
- {
- short resRefNum, index, efntSize;
- FInfo skinny;
- OSErr goof;
- Handle myETHdl, myEFHdl;
- short numFiles = 0;
-
- InitGraf((Ptr) &qd.thePort); /* for Window Manager calls in spaceWidth() */
- SetFScaleDisable(true); /* per the MPW Manual */
- InitCursorCtl(nil);
- /*SetResLoad(false);*/
-
- myETAB.tSize = DEFTAB; /* set default values for resource fields */
- myEFNT.fSize = DEFSZ;
- strcpy(myEFNT.fName, defFont);
-
- ReadCommandLine(argc,argv); /* see if they get overridden by user */
-
- myETAB.mystery = spaceWidth(); /* set this based on actual font & size */
-
- for (index = 1; index < argc; index++)
- {
- RotateCursor(32);
- if (strlen(argv[index]) == 0) /* how could this happen? */
- continue;
- if (*argv[index] == '-') /* it's an option */
- {
- SkipOption(argv[index], &index); /* so skip the next arg as well */
- continue;
- }
- else /* it should be a filename */
- {
- if (goof = getfinfo(argv[index], 0, &skinny)) /* not noErr? */
- {
- ReportError(goof, argv[index]);
- continue;
- }
- else /* goof was zero */
- {
- if (skinny.fdType == 'TEXT' && skinny.fdCreator == 'KAHL')
- { /* correct type & creator? */
- if (openrf(argv[index], 0, &resRefNum) == noErr)
- { /* a resource fork already existed, so wipe it out */
- goof = SetEOF(resRefNum, 0);
- goof = FSClose(resRefNum);
- }
- createresfile(argv[index]); /* start fresh */
- resRefNum = openresfile(argv[index]);
- UseResFile(resRefNum);
- myETHdl = NewHandle(sizeof(etabData));
- BlockMove((Ptr)&myETAB, *myETHdl, sizeof(etabData));
- myEFHdl = NewHandle(efntSize = (2 + 1 + myEFNT.fName[0]));
- /* size word plus string's length + 1 for the length byte */
- BlockMove((Ptr)&myEFNT, *myEFHdl, efntSize);
- AddResource(myETHdl, 'ETAB', 1004, "");
- AddResource(myEFHdl, 'EFNT', 1003, "");
- CloseResFile(resRefNum);
- numFiles++; /* keep track of files we've fixed */
- } /* if skinny.fdType... */
- }
- } /* else */
- } /* for */
- printf("%d files changed\n", numFiles); /* report back on our accomplishments */
- }
-
-
-